/* eslint-disable prettier/prettier */
import { defineStore } from 'pinia'
import {
  trainingProgramsAll,
  employeeSearch,
  trainingProgramsEdit,
  trainingProgramsAdd,
  trainingProgramsDetail,
  trainingProgramsDel,
  evalutionAll,
  evalutionEdit,
  developPlanAll,
  developPlanlike,
  developPlanDetail,
  developPlanEdit,
  pdNameAll,
} from '../../api/professional'

export const useProfessionalStore = defineStore(
  'DevelopPlan',
  {
    state: () => ({
      // TrainingPrograms 培训计划表
      trainingPrograms: {
        scheme: [],
        total: 0
      },
      // 部门职位员工表
      dpes: {
        employee: {},
        total: 0
      },

      // Evaluation 培训评估表
      evalutions: {
        scheme: [],
        total: 0
      },

      // DevelopmentPlanning 员工职业发展表
      // 全部数据
      developPlans: {
        develop: [],
        total: 0
      },
      // 详情数据
      developPlansDetail: {
        id: 0,
        detail: '',
        status: 1,
        name: '',
        department: '',
        position: '',
        eid: 0
      },
      pdNames: {
        total: 0,
        pdName: {}
      }
    }),
    actions: {
      // TrainingPrograms 培训计划表
      // 获取培训表所有数据
      async fetchTrainingProgramsAll() {
        try {
          const res = await trainingProgramsAll()

          Object.assign(this.trainingPrograms, res.data) //整体赋值

          console.log('后端获取的trainingPrograms', this.trainingPrograms)
        } catch (error) {
          this.error = 'Failed to fetch infos.'
        }
      },
      // 获取部门职位员工表
      async fetchDPEs() {
        try {
          const res = await employeeSearch()

          Object.assign(this.dpes, res.data) //整体赋值

          console.log('后端获取的部门职位员工表', this.dpes)
        } catch (error) {
          this.error = 'Failed to fetch infos.'
        }
      },
      // 信息编辑
      async editTraining(data) {
        try {
          await trainingProgramsEdit(data.value) // 等待异步操作完成
          console.log('Edit complited!')
          const response = await trainingProgramsAll() // 重新获取全部信息
          this.trainingPrograms = response.data // 更新响应式变量
        } catch (error) {
          console.error('Failed:', error)
        }
      },
      // 信息添加
      async addTraining(data) {
        try {
          console.log('后端的add data', data.value)
          await trainingProgramsAdd(data.value) // 等待异步操作完成
          console.log('Add complited!')
          const response = await trainingProgramsAll() // 重新获取全部信息
          this.trainingPrograms = response.data // 更新响应式变量
        } catch (error) {
          console.error('Failed:', error)
        }
      },
      // 信息查询
      async trainingDetail(id) {
        try {
          console.log('后端id', id)
          const res = await trainingProgramsDetail(id) // 等待异步操作完成
          console.log('Search complited!')
          console.log('后端查询的数据', res.data.scheme)
          return res.data.scheme
        } catch (error) {
          console.error('Failed:', error)
        }
      },
      // 信息删除
      async delTraining(ids) {
        try {
          console.log('ids', ids)
          await trainingProgramsDel(ids) // 等待异步操作完成
          console.log('Del complited!')

          const response = await trainingProgramsAll() // 重新获取全部信息
          this.trainingPrograms = response.data // 更新响应式变量
        } catch (error) {
          console.error('Failed:', error)
        }
      },

      // Evaluation 培训评估表
      // 获取评估表所有数据
      async fetchEvalutionAll() {
        try {
          const res = await evalutionAll()
          Object.assign(this.evalutions, res.data) //整体赋值

          console.log('后端获取的evolutions', this.evalutions)
        } catch (error) {
          this.error = 'Failed to fetch infos.'
        }
      },
      // 信息编辑
      async editEvalution(data) {
        try {
          console.log(data)
          await evalutionEdit(data) // 等待异步操作完成

          console.log('Edit complited!')
          const response = await evalutionAll() // 重新获取全部信息
          this.evalutions = response.data // 更新响应式变量
        } catch (error) {
          console.error('Failed:', error)
        }
      },

      // DevelopmentPlanning 员工职业发展表
      // 获取全部员工职业发展表数据
      async fetchDevelopPlanAll() {
        try {
          const res = await developPlanAll()
          const res1 = await pdNameAll()
          console.log('后端部门', res1.data)

          Object.assign(this.developPlans, res.data) //整体赋值
          Object.assign(this.pdNames, res1.data)

          console.log(this.pdNames, 'this.pdNames')
          console.log(this.developPlans, 'this.developPlans')
        } catch (error) {
          this.error = 'Failed to fetch infos.'
        }
      },
      // 查看详情
      async seeDetail(id) {
        try {
          const response = await developPlanDetail(id) // 等待异步操作完成
          this.developPlansDetail = response.data.develop // 更新响应式变量
        } catch (error) {
          console.error('Failed to fetch detail:', error)
        }
        console.log(this.developPlansDetail, '后端')
      },
      // 编辑保存
      async save(data) {
        try {
          await developPlanEdit(data.value) // 等待异步操作完成
          const response = await developPlanAll() // 等待异步操作完成
          // console.log(response.data);
          this.developPlans = response.data // 更新响应式变量
        } catch (error) {
          console.error('Failed:', error)
        }
      }
    }
  }
  // {
  //     persist: true
  // }
)
